//
//  RCShopManageDetailCell.m
//  RocketProject
//
//  Created by Wu Chengwei on 13-10-2.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCShopManageDetailCell.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "TTTAttributedLabel.h"
#import "NSAttributedString+LVAddition.h"
#import <QuartzCore/QuartzCore.h>
#import "NSString+NSString_Ex_.h"
#import "UIImageView+WebCache.h"
#import "RCMapViewController.h"
#import "RCOrderVC.h"
#import "GloableEngineService.h"
#import "AppDelegate.h"
#import "RCMemberVC.h"
#import "UIImageView+WebCache.h"

#define kLineImageFile           @"image_line_type3.png"

#define kBGImageFile             @"image_member_cell_bg.png"
#define kDefaultImageFile        @"image_recommendFood_image_bg.png"
#define kStartActiveImageFile    @"image_start_active.png"
#define kStartInActiveImageFile  @"image_merchant_inactive.png"

@implementation RCShopManageDetailHeaderCell{
    UILabel *_merchantNameLabel;
    UILabel *_serviceLabel;
    UILabel *_priceLabel;
    UILabel *_sanitationLabel;
    UIImageView *_placeImageView;
    UILabel *_distanceLabel;
    UIImageView *_lineImageView;
    UIImageView *_merchantImageView;
    UIButton    *_transParentButton;
    UIImageView *_startImageView1;
    UIImageView *_startImageView2;
    UIImageView *_startImageView3;
    UIImageView *_startImageView4;
    UIImageView *_startImageView5;
    UILabel     *_scoreLabel;
}
@synthesize delegate = _delegate;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        UIImage *bgImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGImageFile ofType:nil]];
        self.layer.contents = (id)bgImage.CGImage;
        [self.contentView setBackgroundColorWithImage:bgImage];
        [self setUpView];
    }
    return self;
}

-(void)dealloc{
    self.delegate = nil;
    [super dealloc];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
    
}


- (void) setUpView
{
    _merchantNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(14, 13, 280, 20)];
    _merchantNameLabel.font = [UIFont fontWithName:kFontNameHeiti size:19];
    _merchantNameLabel.textColor = [UIColor colorWithHex:0xff505050];
    _merchantNameLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_merchantNameLabel];
    [_merchantNameLabel release];
    
    UIImageView *defaultImageView = [[UIImageView alloc]initWithFrame:CGRectMake(14, 44, 100, 70)];
    UIImage *defaultImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kDefaultImageFile ofType:nil]];
    defaultImage = [defaultImage resizableImageWithCapInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [defaultImageView setImage:defaultImage];
    [self addSubview:defaultImageView];
    [defaultImageView release];
    
    _merchantImageView = [[UIImageView alloc]initWithFrame:CGRectMake(17, 47, 94, 64)];
    _merchantImageView.backgroundColor = [UIColor clearColor];
    [self addSubview:_merchantImageView];
    [_merchantImageView release];
    
    _transParentButton = [[UIButton alloc]initWithFrame:_merchantImageView.frame];
    _transParentButton.backgroundColor = [UIColor clearColor];
    [_transParentButton addTarget:self action:@selector(transparentButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    _transParentButton.hidden = YES;
    [self addSubview:_transParentButton];
    [_transParentButton release];
    
    _startImageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(125, 50, 14, 14)];
    _startImageView1.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView1];
    [_startImageView1 release];
    
    _startImageView2 = [[UIImageView alloc]initWithFrame:CGRectMake(140, 50, 14, 14)];
    _startImageView2.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView2];
    [_startImageView2 release];
    
    _startImageView3 = [[UIImageView alloc]initWithFrame:CGRectMake(155, 50, 14, 14)];
    _startImageView3.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView3];
    [_startImageView3 release];
    
    _startImageView4 = [[UIImageView alloc]initWithFrame:CGRectMake(170, 50, 14, 14)];
    _startImageView4.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView4];
    [_startImageView4 release];
    
    _startImageView5 = [[UIImageView alloc]initWithFrame:CGRectMake(185, 50, 14, 14)];
    _startImageView5.backgroundColor = [UIColor clearColor];
    [self addSubview:_startImageView5];
    [_startImageView5 release];
    
    _scoreLabel = [[UILabel alloc]initWithFrame:CGRectMake(202, 50, 20, 14)];
    _scoreLabel.backgroundColor = [UIColor clearColor];
    _scoreLabel.textColor = [UIColor redColor];
    _scoreLabel.font = [UIFont fontWithName:kFontNameHelvetica size:14];
    [self addSubview:_scoreLabel];
    [_scoreLabel release];
    
//    UIImage *img = [UIImage imageNamed:@"image_allCity_district"];
//    _placeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(253, 50, img.size.width, img.size.height)];
//    _placeImageView.image = img;
//    [self addSubview:_placeImageView];
//    [_placeImageView release];
//    
//    
//    _distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(253+img.size.width+5, 50, 70, 14)];
//    _distanceLabel.font = [UIFont fontWithName:kFontNameHelvetica size:12];
//    _distanceLabel.textColor = [UIColor colorWithHex:0xff989797];
//    _distanceLabel.backgroundColor = [UIColor clearColor];
//    [self addSubview:_distanceLabel];
//    [_distanceLabel release];
    
    _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(125, 75, 200, 12)];
    _priceLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _priceLabel.textColor = [UIColor colorWithHex:0xff989797];
    _priceLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:_priceLabel];
    [_priceLabel release];
    
    _sanitationLabel = [[UILabel alloc] initWithFrame:CGRectMake(125, 95, 60, 12)];
    _sanitationLabel.backgroundColor = [UIColor clearColor];
    _sanitationLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
    _sanitationLabel.textColor = [UIColor colorWithHex:0xff989797];
    [self addSubview:_sanitationLabel];
    [_sanitationLabel release];
    
    _serviceLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 95, 60, 12)];
    _serviceLabel.font = [UIFont fontWithName:kFontNameHeiti size:12];
    _serviceLabel.textColor = [UIColor colorWithHex:0xff989797];
    _serviceLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:_serviceLabel];
    [_serviceLabel release];
    
    
    
    _lineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 129, 320, 1)];
    _lineImageView.backgroundColor = [UIColor clearColor];
    NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:kLineImageFile];
    [_lineImageView setImage:[UIImage imageWithContentsOfFile:filePath]];
    [self.contentView addSubview:_lineImageView];
    [_lineImageView release];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
}
- (void) bindData:(RCShopManagerDataModel *)detailModel
{
    if (nil == detailModel) {
        return;
    }
    _merchantNameLabel.text = detailModel.merchantName;
    
    NSInteger score = detailModel.merchantScore;
    NSString *resourcePath = [[NSBundle mainBundle]resourcePath];
    UIImage *activeImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kStartActiveImageFile]];
    UIImage *inactiveImage = [UIImage imageWithContentsOfFile:[resourcePath stringByAppendingPathComponent:kStartInActiveImageFile]];
    [_startImageView1 setImage:score > 0 ? activeImage : inactiveImage];
    [_startImageView2 setImage:score > 1 ? activeImage : inactiveImage];
    [_startImageView3 setImage:score > 2 ? activeImage : inactiveImage];
    [_startImageView4 setImage:score > 3 ? activeImage : inactiveImage];
    [_startImageView5 setImage:score > 4 ? activeImage : inactiveImage];
    
    _scoreLabel.text = [NSString stringWithFormat:@"%d",score];
    
    
//    NSString *distance = nil;
//    if ([detailModel.merchantDistance floatValue] > 1000) {
//        float distanceKm = [detailModel.merchantDistance floatValue]/1000.0;
//        if ( ((int)distanceKm) > 999 )
//            distance = [NSString stringWithFormat:@"%d+km",999];
//        else
//            distance = [NSString stringWithFormat:@"%.1fkm",distanceKm];
//    }else{
//        distance = [NSString stringWithFormat:@"%@m",detailModel.merchantDistance];
//    }
//    _distanceLabel.text = distance;
    
    _transParentButton.hidden = YES;
    if (![NSString stringIsNullOrEmpty:detailModel.merchantImageUrl]) {
        [_merchantImageView setImageWithURL:[NSURL URLWithString:detailModel.merchantImageUrl] placeholderImage:[UIImage imageNamed:kDefaultImageFile]];
        _transParentButton.hidden = NO;
    }
    
    if (![NSString stringIsNullOrEmpty:detailModel.merchantPrice]) {
        _priceLabel.text = [NSString stringWithFormat:@"人均：￥%@",detailModel.merchantPrice];
    }else{
        _priceLabel.text = @"";
    }
    
    if (![NSString stringIsNullOrEmpty:detailModel.merchantSanitation]) {
        _sanitationLabel.text = [NSString stringWithFormat:@"环境：%@",detailModel.merchantSanitation];
    }else{
        _sanitationLabel.text = @"";
    }
    
    if (![NSString stringIsNullOrEmpty:detailModel.merchantService]) {
        _serviceLabel.text = [NSString stringWithFormat:@"服务：%@",detailModel.merchantService];
    }else{
        _serviceLabel.text = @"";
    }
    
}

-(void)transparentButtonClicked{
    if (_delegate && [_delegate respondsToSelector:@selector(gotoPictureAlbumView)]) {
        [_delegate gotoPictureAlbumView];
    }
}

@end


@implementation RCShopManageDetailOnelineCell{
    UIImageView *_iconImageView;
    UILabel *_typeTitleLabel;
    TTTAttributedLabel *_typeContentLabel;
    RCShopManagerDetailItem *_dataModel;
    NSString *_merchantName;
    NSString *_merchantID;
    
}

@synthesize dataModel = _dataModel;
@synthesize cellType = _cellType;
@synthesize parentViewController = _parentViewController;
@synthesize merchantName = _merchantName;
@synthesize merchantID = _merchantID;

- (id) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImage *bgImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGImageFile ofType:nil]];
        self.layer.contents = (id)bgImage.CGImage;
        [self.contentView setBackgroundColorWithImage:bgImage];
        [self setUpView];
    }
    return self;
}

-(void)dealloc{
    self.dataModel = nil;
    self.merchantID = nil;
    self.merchantName = nil;
    self.parentViewController = nil;
    [super dealloc];
}


- (void) setUpView
{
    
    _iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(14, 13, 8, 10)];
    //    _iconImageView.image = img;
    [self addSubview:_iconImageView];
    [_iconImageView release];
    
    
    _typeTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 13, 60, 15)];
    _typeTitleLabel.font = [UIFont fontWithName:kFontNameHeiti size:15];
    _typeTitleLabel.textColor = [UIColor colorWithHex:0xff505050];
    _typeTitleLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:_typeTitleLabel];
    [_typeTitleLabel release];
    
    
    _typeContentLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(30, 13, 280, 100)];
    _typeContentLabel.leading = 6;
    _typeContentLabel.numberOfLines = 0;
    _typeContentLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:13];
    _typeContentLabel.textColor = [UIColor colorWithHex:0xff505050];
    _typeContentLabel.backgroundColor = [UIColor clearColor];
    _typeContentLabel.userInteractionEnabled = NO;
    [self addSubview:_typeContentLabel];
    [_typeContentLabel release];
    
    
    
}
- (void) bindData:(RCShopManagerDetailItem *)detailModel
{
    if (nil == detailModel) {
        return;
    }
    self.dataModel = detailModel;
    _typeContentLabel.text = [NSString stringWithFormat:@"%@：%@",detailModel.title,detailModel.content];
    
    
    CGFloat contentWidth = _typeContentLabel.frame.size.width;
    
    NSAttributedString *attributedStr = _typeContentLabel.attributedText;
    CGSize restSize = [attributedStr sizeWithConstrainedToSize:CGSizeMake(contentWidth, 2000) Leading:6];
    _typeContentLabel.frame = CGRectMake(_typeContentLabel.frame.origin.x, 13, contentWidth, restSize.height);
    
    
    
    if (detailModel.type == kShopManagerDetailType_Phone) {
        
        NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"image_shopManager_phone.png"];
        _iconImageView.frame = CGRectMake(14, 13, 13, 13);
        [_iconImageView setImage:[UIImage imageWithContentsOfFile:filePath]];
        
    }else if (detailModel.type == kShopManagerDetailType_Address){
        
        NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"image_shopManager_address.png"];
        _iconImageView.frame = CGRectMake(14, 11, 10, 14);
        [_iconImageView setImage:[UIImage imageWithContentsOfFile:filePath]];
    
    }else if (detailModel.type == kShopManagerDetailType_Opentime){
        
        NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"image_shopManager_opentime.png"];
        _iconImageView.frame = CGRectMake(14, 15, 13, 13);
        [_iconImageView setImage:[UIImage imageWithContentsOfFile:filePath]];

    }else if (detailModel.type == kShopManagerDetailType_Character){
    
        NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"image_shopManager_character.png"];
        _iconImageView.frame = CGRectMake(14, 15, 13, 12);
        [_iconImageView setImage:[UIImage imageWithContentsOfFile:filePath]];
        
    }else if (detailModel.type == kShopManagerDetailType_Atmosphere){
        NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"image_shopManager_atomosphere.png"];
        _iconImageView.frame = CGRectMake(14, 15, 12, 13);
        [_iconImageView setImage:[UIImage imageWithContentsOfFile:filePath]];

        
    }else if (detailModel.type == kShopManagerDetailType_Description){
        NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"image_shopManager_description.png"];
        _iconImageView.frame = CGRectMake(14, 15, 14, 13);
        [_iconImageView setImage:[UIImage imageWithContentsOfFile:filePath]];
        
        
    }
}



+ (CGFloat) cellHeightWithString:(RCShopManagerDetailItem *)detailModel
{
    if (nil == detailModel) {
        return 41;
    }
    
    TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(30, 13, 280, 100)];
    label.leading = 6;
    label.numberOfLines = 0;
    label.font = [UIFont fontWithName:kFontNameLightHeiti size:13];
    label.textColor = [UIColor colorWithHex:0xff505050];
    label.backgroundColor = [UIColor clearColor];
    
    
    label.text = detailModel.content;
    NSAttributedString *attributedStr = label.attributedText;
    CGSize restSize = [attributedStr sizeWithConstrainedToSize:CGSizeMake(label.frame.size.width, 2000) Leading:6];
    [label release];
    
    return 13 + restSize.height + 13;
    
}


@end


@implementation RCShopManageDetailTwolineCell{
    UIImageView *_iconImageView;
    UILabel *_typeTitleLabel;
    TTTAttributedLabel *_typeContentLabel;
    UIImageView *_lineImageView;
}

- (id) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImage *bgImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:kBGImageFile ofType:nil]];
        self.layer.contents = (id)bgImage.CGImage;
        [self.contentView setBackgroundColorWithImage:bgImage];
        [self setUpView];
    }
    return self;
}


- (void) setUpView
{
    
    _iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(14, 13, 12, 12)];
    _iconImageView.backgroundColor = [UIColor clearColor];
    [self addSubview:_iconImageView];
    [_iconImageView release];
    
    _typeTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 13, 150, 16)];
    _typeTitleLabel.font = [UIFont fontWithName:kFontNameHeiti size:15];
    _typeTitleLabel.textColor = [UIColor colorWithHex:0xff505050];
    _typeTitleLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:_typeTitleLabel];
    [_typeTitleLabel release];
    
    
    _typeContentLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(30, 34, 280, 200)];
    _typeContentLabel.font = [UIFont fontWithName:kFontNameHeiti size:13];
    _typeContentLabel.textColor = [UIColor colorWithHex:0xff989797];
    _typeContentLabel.numberOfLines = 0;
    _typeContentLabel.leading = 6;
    _typeContentLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:_typeContentLabel];
    [_typeContentLabel release];
    
    _lineImageView = [[UIImageView alloc]init];
    _lineImageView.backgroundColor = [UIColor clearColor];
    NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:kLineImageFile];
    [_lineImageView setImage:[UIImage imageWithContentsOfFile:filePath]];
    [self addSubview:_lineImageView];
    [_lineImageView release];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
}
- (void) bindData:(RCShopManagerDetailItem *)detailModel
{
    if (nil == detailModel) {
        return;
    }
    
    _typeContentLabel.textColor = [UIColor colorWithHex:0xff989797];
    
    
    _typeTitleLabel.hidden = YES;
    _iconImageView.hidden = YES;
    
    if (![NSString stringIsNullOrEmpty:detailModel.title]) {
        _typeTitleLabel.text = detailModel.title;
        
        _typeTitleLabel.hidden = NO;
        _iconImageView.hidden = NO;

        if (detailModel.type == kShopManagerDetailType_Phone) {
            
            NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"image_shopManager_phone.png"];
            _iconImageView.frame = CGRectMake(14, 15, 13, 13);
            [_iconImageView setImage:[UIImage imageWithContentsOfFile:filePath]];
            
        }else if (detailModel.type == kShopManagerDetailType_Address){
            
            NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"image_shopManager_address.png"];
            _iconImageView.frame = CGRectMake(14, 15, 10, 14);
            [_iconImageView setImage:[UIImage imageWithContentsOfFile:filePath]];
            
        }else if (detailModel.type == kShopManagerDetailType_Opentime){
            
            NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"image_shopManager_opentime.png"];
            _iconImageView.frame = CGRectMake(14, 15, 13, 13);
            [_iconImageView setImage:[UIImage imageWithContentsOfFile:filePath]];
            
        }else if (detailModel.type == kShopManagerDetailType_Character){
            
            NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"image_shopManager_character.png"];
            _iconImageView.frame = CGRectMake(14, 15, 13, 12);
            [_iconImageView setImage:[UIImage imageWithContentsOfFile:filePath]];
            
        }else if (detailModel.type == kShopManagerDetailType_Atmosphere){
            NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"image_shopManager_atomosphere.png"];
            _iconImageView.frame = CGRectMake(14, 15, 12, 13);
            [_iconImageView setImage:[UIImage imageWithContentsOfFile:filePath]];
            
            
        }else if (detailModel.type == kShopManagerDetailType_Description){
            NSString *filePath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:@"image_shopManager_description.png"];
            _iconImageView.frame = CGRectMake(14, 15, 14, 13);
            [_iconImageView setImage:[UIImage imageWithContentsOfFile:filePath]];
            
            
        }
    }else{
        _typeTitleLabel.text = @"";
        [_iconImageView setImage:nil];
    }
    
    
    
    _typeContentLabel.text = detailModel.content;
    
    NSAttributedString *attributedStr = _typeContentLabel.attributedText;
    CGSize restSize = [attributedStr sizeWithConstrainedToSize:CGSizeMake(280, 2000) Leading:6];
    _typeContentLabel.frame = CGRectMake(30, 34, 280, restSize.height);
    _lineImageView.frame  = CGRectMake(0, 34+restSize.height+14, 320, 1);
}

+ (CGFloat) cellHeightWithString:(NSString *)str
{
    TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(30, 34, 280, 200)];
    label.font = [UIFont fontWithName:kFontNameHeiti size:13];
    label.textColor = [UIColor colorWithHex:0xff989797];
    label.numberOfLines = 0;
    label.leading = 6;
    label.backgroundColor = [UIColor clearColor];
    label.text = str;
    NSAttributedString *attributedStr = label.attributedText;
    CGSize restSize = [attributedStr sizeWithConstrainedToSize:CGSizeMake(280, 2000) Leading:6];
    [label release];
    return 34 + restSize.height + 15;
    
}


@end
